<?php defined('SYSPATH') or die('No direct script access.');

class Calendar_Category_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();

		$this->template->menu = new View('menu');
	}
	
	public function index()
	{		
		$model = new Calendar_Category_Model();
		
		$table = new View('calendar/category_lists');
		$table->caption = 'Calendar Category List';
		$table->datas = $model->find_all();;
		
		$this->template->content = $table->render();
	}
	
	public function create()
	{
		$form = new Forge(NULL, 'New Calendar Category');
		$form->input('name')->label(TRUE);
		$form->input('title_cn')->label('Chinese Title')->rules('required');
		$form->input('title_en')->label('English Title')->rules('required');
		$form->submit('Save');
 
		if($form->validate())
		{
			//Load model and set values
			$attribute = new Calendar_Category_Model();
			$attribute->name = trim($form->name->value) == '' ? $form->title_cn->value : $form->name->value;
			$attribute->title_cn = $form->title_cn->value;
			$attribute->title_en = $form->title_en->value;
 
			//Save form into table
			if($attribute->save())
			{
				url::redirect('calendar_category');
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->html();
		}
	}
	
	public function edit($id)
	{
		$result = new Calendar_Category_Model($id);

		$form = new Forge(NULL, 'Edit Calendar Category');
		$form->input('name')->label(TRUE)->value($result->name);
		$form->input('title_cn')->label('Chinese Title')->rules('required')->value($result->title_cn);
		$form->input('title_en')->label('English Title')->rules('required')->value($result->title_en);
		$form->submit('Save');
 
		if($form->validate())
		{
			$result->name = $form->name->value;
			$result->title_cn = $form->title_cn->value;
			$result->title_en = $form->title_en->value;
 
			//Save form into table
			if($result->save())
			{
				url::redirect('calendar_category');
			}
			else
			{
				echo 'no saving today';
			}
		}
		else
		{
		    $this->template->content = $form->html();
		}
	}
	
	public function delete($id)
	{
		$result = new Calendar_Category_Model($id);
		$result->delete();
		
		url::redirect('calendar_category');
	}

}